﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FirmaTransportowa.ViewsInterfaces;
using FirmaTransportowa;
using System.Windows.Forms;

namespace FirmaTransportowa.Presenters
{
    public class CashierPanelPresenter
    {
        CashierPanel view;

        Package currentPackage;
        BindingSource packageList;

        public CashierPanelPresenter(User user)
        {
            this.view = new CashierPanel(user.id);            

            //register events
            this.view.ObjectSelected += OnPackageSelected;
            this.view.DoubleClick += OnDoubleClick;
            this.view.NewPackage += OnNewPackage;
            this.view.Delete += OnDelete;

            currentPackage = null;

            packageList = Package.getPackagesForCashier();
            view.LoadObjects(packageList);
            view.Show();
        }     

        public void OnPackageSelected()
        {
            if (this.view.selectedId > 0)
            {
                currentPackage = Package.getById(view.selectedId);
            }
        }

        private void OnDoubleClick()
        {
            if (currentPackage != null)
            {
                CashierPackageDetailsPresenter cashierDetailsPresenter;
                if (currentPackage.status == PackageStatus.NEW)
                    cashierDetailsPresenter = new CashierPackageDetailsPresenter(this, currentPackage);
                else
                    MessageBox.Show("Nie możesz edytować tej paczki","Błąd",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void OnNewPackage()
        {
            CashierPackageDetailsPresenter cashierDetailsPresenter = new CashierPackageDetailsPresenter(this);
        }

        private void OnDelete()
        {
            if (currentPackage != null)
            {
                currentPackage.delete();
                refresh();
            }
        }

        public void refresh()
        {
            packageList = Package.getPackagesForCashier();
            view.dataGridView1.DataSource = null;
            view.LoadObjects(packageList);
        }

    }
}
